package com.franklin.ideaplugin.maphelper.beans.colmember;

import com.franklin.ideaplugin.maphelper.col.declaration.IColDeclaration;
import com.franklin.ideaplugin.maphelper.col.method.IColInsertMethod;
import com.franklin.ideaplugin.maphelper.resolver.AbstractColMemberResolverFactory;
import com.franklin.ideaplugin.maphelper.utils.PsiColUtils;
import com.intellij.psi.PsiExpression;
import lombok.Data;

import java.util.Collection;
import java.util.Objects;

/**
 * map成员
 *
 * @author Ye Junhui
 * @since 2024/3/5
 */
@Data
public class PsiColMember implements IColMember{

    /**
     * 对象
     */
    private PsiExpression[] psiExpressions;

    /**
     * 插入方法
     */
    private IColInsertMethod colInsertMethod;


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        PsiColMember psiColMember = (PsiColMember) o;
        return Objects.equals(psiExpressions[0].getText(), psiColMember.psiExpressions[0].getText());
    }

    @Override
    public int hashCode() {
        return Objects.hash(psiExpressions[0].getText());
    }

    @Override
    public String getKeyText() {
        return this.psiExpressions[0].getText();
    }

    @Override
    public String getValueText() {
        return getValueExpression().getText();
    }

    @Override
    public String getValueClass() {
        return getValueExpression().getType().getCanonicalText();
    }

    @Override
    public String getValueClassSimple() {
        return getValueExpression().getType().getPresentableText();
    }

    @Override
    public Collection<IColMember> getChildren(AbstractColMemberResolverFactory colMemberResolverFactory) {
        PsiExpression valueExpression = getValueExpression();
        IColDeclaration realChildMapDeclaration = PsiColUtils.findColExpressionDeclaration(valueExpression);
        return colMemberResolverFactory.resolveExpression(valueExpression,realChildMapDeclaration);
    }

    /**
     * value表达式
     * @return
     */
    public PsiExpression getValueExpression(){
        return this.psiExpressions[this.colInsertMethod.getInsertParamIndex()];
    }
}
